package com.bsoft.exchange.service;

import com.alibaba.fastjson.JSONObject;
import com.bsoft.exchange.dao.his.PrescriptionAndEquipmentMapper;
import com.bsoft.exchange.dao.his.ElectronicInvoiceMapper;
import com.bsoft.exchange.dao.portal.HospitalInformationMapper;
import com.bsoft.exchange.domain.*;
import com.bsoft.exchange.pojo.BillPicUrlPojo;
import com.bsoft.exchange.pojo.ElectronicInvoicePojo;
import com.bsoft.exchange.util.*;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 创建日期 : 2020/7/9
 * 创建人 : 赵太行
 * 电子发票
 */
@Service
public class ElectronicInvoiceService {
    @Resource
    ElectronicInvoiceMapper electronicInvoiceMapper;

    @Resource
    PrescriptionAndEquipmentMapper cfAndYjMapper;

    @Resource
    CacheSupport cacheSupport;

    @Resource
    HospitalInformationMapper hospitalInformationMapper;
    @Resource
    PublicService publicService;

    @Resource
    ElectronicUtil electronicUtil;

    @Value("${electronic.url}")
    private String electronicUrl;
    @Resource
    Gson gson;
    @Transactional(value="hisTransactionManager",rollbackFor = Exception.class,readOnly=true)
    public BaseResponse getMzInfo(String organizationId, String businessNo) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            InvoiceEBillOutpatientDomain wy = electronicInvoiceMapper.selectSole(businessNo, organizationId, 1);
            if (wy != null) {
                baseResponse.setCode(Constant.errorTag);
                baseResponse.setMessage("请勿重复提交");
//            baseResponse.setData(wy);
                return baseResponse;
            }
            OutpatientInfo mzxxDomain = electronicInvoiceMapper.getMzxxList(organizationId, businessNo);
            if (mzxxDomain == null) {
                baseResponse.setCode(Constant.errorTag);
                baseResponse.setMessage("未查询到" + businessNo + "门诊收费信息");
                return baseResponse;
            }
            //如果该条发票信息的操作工号没有在黑名单中
            List<Prescription02> cf02Data = cfAndYjMapper.selectCfByFphm(organizationId, businessNo);
            List<Equipment02> yj02Data = cfAndYjMapper.selectYjByFphm(organizationId, businessNo);
            List<DrugInfo> ypxxDomainList = publicService.getDrugInfo();
            //设置visDetails内容
            if (cf02Data == null && yj02Data == null) {
                baseResponse.setCode(Constant.errorTag);
                baseResponse.setMessage("未查询到" + mzxxDomain.getFphm() + "发票的处方或者医技信息");
                return baseResponse;
            }
            JSONObject body = electronicUtil.getMzJson(organizationId, businessNo, cf02Data, yj02Data, mzxxDomain, ypxxDomainList, organizationId);
            String message = HttpClientUtil.doPost(electronicUrl, body, "utf-8");

            BaseResponse mybase=gson.fromJson(message,BaseResponse.class);
//            baseResponse.setData(batchCodeDomainBaseDomain);
            baseResponse.setCode(mybase.getCode());
            baseResponse.setMessage(mybase.getMessage());
        }catch (IOException e){
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage(e.getLocalizedMessage());
            return baseResponse;
        }
        return baseResponse;
    }
    /**
     * 获取H5页面
     */
    public BaseResponse getH5Url(BillPicUrlPojo pojo){
        BaseResponse baseResponse=new BaseResponse();
        String url=electronicInvoiceMapper.h5url(pojo.getBillBatchCode(),pojo.getBillNo(),pojo.getRandom());
        baseResponse.setCode(Constant.successResponse);
        if (TextUtils.isEmpty(url)){
            baseResponse.setMessage("找不到该票据信息");
            return baseResponse;
        }
        baseResponse.setMessage("查询成功");
        baseResponse.setData(url);
        return baseResponse;
    }
    private List<String> brdaList;

    public BaseResponse<List<ElectronicInvoiceDomain>> queryInvoiceList(ElectronicInvoicePojo electronicInvoicePojo) throws SQLException {
        BaseResponse<List<ElectronicInvoiceDomain>> baseResponse = new BaseResponse<>();

        //获取brid列表
        if (!electronicInvoicePojo.getQueryType().equals(1)) {
            brdaList = publicService.getBRIDist(electronicInvoicePojo.getOrganizationId(),
                    electronicInvoicePojo.getQueryType(), electronicInvoicePojo.getPatientIdentityCardList(), electronicInvoicePojo.getPatientMedicalCardList());
        } else {
            brdaList = electronicInvoicePojo.getPatientIdList();
        }
        if (brdaList == null) {
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("数据库查询失败,请检查参数设置");
            return baseResponse;
        }
        if (brdaList.size() == 0) {
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("获取不到该病人信息");
            return baseResponse;
        }

        //数据分页
        PageHelper.startPage(electronicInvoicePojo.getPageNo(), electronicInvoicePojo.getPageSize());
        //获取发票信息列表
        List<ElectronicInvoiceDomain> electronicInvoiceDomainList = electronicInvoiceMapper.getElectronicInvoiceList(electronicInvoicePojo.getOrganizationId(), brdaList);
        if (electronicInvoiceDomainList.size() == 0) {
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("暂无数据");
            return baseResponse;
        }

        //获取机构名称列表
        List<HospitalInformation> hospitalInformationList = cacheSupport.getJgmcList(electronicInvoicePojo.getOrganizationId(), electronicInvoicePojo.getOrganizationId() + "jgmc", hospitalInformationMapper);
        //根据sffs和发票号码给发票类别赋值
        for (ElectronicInvoiceDomain electronicInvoiceDomain : electronicInvoiceDomainList) {
            int sffs = Integer.valueOf(electronicInvoiceDomain.getSffs());
            if (sffs > 0) {
                electronicInvoiceDomain.setInvoiceType(2);
            } else if (sffs == 0) {
                electronicInvoiceDomain.setInvoiceType(1);
            } else {
                if (TextUtils.isEmpty(electronicInvoiceDomain.getInvoiceNumber())) {
                    electronicInvoiceDomain.setInvoiceType(1);
                } else {
                    electronicInvoiceDomain.setInvoiceType(2);
                }
            }
            //给机构名称赋值
            for (HospitalInformation jgxx : hospitalInformationList) {
                if (electronicInvoiceDomain.getOrganizationId().equals(jgxx.getOrganizationId())) {
                    electronicInvoiceDomain.setOrganizationName(jgxx.getOrganizationName());
                }
            }
        }
        baseResponse.setMessage("查询成功");
        baseResponse.setCode(Constant.successTag);
        baseResponse.setData(electronicInvoiceDomainList);
        return baseResponse;
    }
}
